#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
// 可以用使用的步数表示当前的位置
// 从卡片的张数可以推测出来
const int N = 400, M = 45;
int scores[N];
int a[5]; // 存放每种卡牌的张数
int dp[M][M][M][M];
int n, m;

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        cin >> scores[i];
    // 统计每种卡牌有多少种
    for (int i = 0; i < m; ++i)
    {
        int tmp;
        cin >> tmp;
        a[tmp]++;
    }
    dp[0][0][0][0] = scores[0];
    for (int i = 0; i <= a[1]; ++i)
        for (int j = 0; j <= a[2]; ++j)
            for (int k = 0; k <= a[3]; ++k)
                for (int t = 0; t <= a[4]; ++t)
                {
                    int score = scores[i + 2 * j + 3 * k + 4 * t];
                    int &v = dp[i][j][k][t];
                    if (i)
                        v = max(v, dp[i - 1][j][k][t] + score);
                    if (j)
                        v = max(v, dp[i][j - 1][k][t] + score);
                    if (k)
                        v = max(v, dp[i][j][k - 1][t] + score);
                    if (t)
                        v = max(v, dp[i][j][k][t - 1] + score);
                }
    cout << dp[a[1]][a[2]][a[3]][a[4]] << endl;

    return 0;
}
